package cn.djb.day26;

class Solution_2 {

    public int strToInt(String str) {
        //去空格
        char[] chars = str.trim().toCharArray();
        if (chars.length == 0) return 0;
        int i = 1, sign = 1;
        if (chars[0] == '-') {
            sign = -1;
        } else if (chars[0] != '+') {
            i = 0;
        }
        int res = 0, max = Integer.MAX_VALUE / 10;
        for (int j = i; j < chars.length; j++) {
            char c = chars[j];
            if (c < '0' || c > '9') break;
            if (res > max || res == max && c > '7') return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;

            res = res * 10 + (c - '0');
        }

        return sign * res;

    }

}